package com.zg.shop.order;

import java.util.List;import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zg.shop.utils.PageHibernateCallback;

public class OrderDao extends HibernateDaoSupport{

	public Integer saveOrder(Order order) {
		Integer oid = (Integer) this.getHibernateTemplate().save(order);
		return oid;
	}

	public Order findByOid(Integer oid) {
		return this.getHibernateTemplate().get(Order.class, oid);
	}

	public void update(Order currOrder) {
		this.getHibernateTemplate().update(currOrder);
	}

	public List<Order> findByUid(Integer uid) {
		List<Order> orders = this.getHibernateTemplate().find("from Order o where o.user.uid = ?", uid);
		return orders;
	}

	public Integer findCount() {
		String hql = "select count(*) from Order";
		List<Long> list = this.getHibernateTemplate().find(hql);
		if(list.size() > 0){
			return list.get(0).intValue();
		}else {
			return null;
		}
	}
	
	public Integer findCount(Integer state) {
		String hql = "select count(*) from Order where state = ?";
		List<Long> list = this.getHibernateTemplate().find(hql,state);
		if(list.size() > 0){
			return list.get(0).intValue();
		}else {
			return null;
		}
	}

	public List<Order> findByPage(Integer begin, Integer climit) {
		String hql = "from Order order by ordertime desc";
		List<Order> orders = this.getHibernateTemplate().execute(new PageHibernateCallback<>(hql, null, begin, climit));
		return orders;
	}
	
	public List<Order> findByPage(Integer begin, Integer climit, Integer state) {
		String hql = "from Order where state = ? order by ordertime desc";
		List<Order> orders = this.getHibernateTemplate().execute(new PageHibernateCallback<>(hql, new Object[]{state}, begin, climit));
		return orders;
	}
}
